From 622b78c0004fd0431c19745f395c7758813bb7a3 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 12 Jan 2012 22:11:41 +0100 Subject: [PATCH] Add GtkImage constructors from resources Atm you can't read back the resource path like you can with filenames. Maybe we should add that. --- gtk/gtkimage.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ gtk/gtkimage.h | 3 ++ 2 files changed, 93 insertions(+) diff --git a/gtk/gtkimage.c b/gtk/gtkimage.c index 057e750523..e7fbf09fa5 100644 --- a/gtk/gtkimage.c +++ b/gtk/gtkimage.c @@ -512,6 +512,43 @@ gtk_image_new_from_file (const gchar *filename) return GTK_WIDGET (image); } +/** + * gtk_image_new_from_resource: + * @resource_path: a resource path + * + * Creates a new #GtkImage displaying the resource file @resource_path. If the file + * isn't found or can't be loaded, the resulting #GtkImage will + * display a "broken image" icon. This function never returns %NULL, + * it always returns a valid #GtkImage widget. + * + * If the file contains an animation, the image will contain an + * animation. + * + * If you need to detect failures to load the file, use + * gdk_pixbuf_new_from_file() to load the file yourself, then create + * the #GtkImage from the pixbuf. (Or for animations, use + * gdk_pixbuf_animation_new_from_file()). + * + * The storage type (gtk_image_get_storage_type()) of the returned + * image is not defined, it will be whatever is appropriate for + * displaying the file. + * + * Return value: a new #GtkImage + * + * Since: 3.4 + **/ +GtkWidget* +gtk_image_new_from_resource (const gchar *resource_path) +{ + GtkImage *image; + + image = g_object_new (GTK_TYPE_IMAGE, NULL); + + gtk_image_set_from_resource (image, resource_path); + + return GTK_WIDGET (image); +} + /** * gtk_image_new_from_pixbuf: * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL @@ -740,6 +777,59 @@ gtk_image_set_from_file (GtkImage *image, g_object_thaw_notify (G_OBJECT (image)); } +/** + * gtk_image_set_from_resource: + * @image: a #GtkImage + * @resource_path: (allow-none): a resource path or %NULL + * + * See gtk_image_new_from_resource() for details. + **/ +void +gtk_image_set_from_resource (GtkImage *image, + const gchar *resource_path) +{ + GtkImagePrivate *priv; + GdkPixbuf *pixbuf; + GInputStream *stream; + + g_return_if_fail (GTK_IS_IMAGE (image)); + + priv = image->priv; + + g_object_freeze_notify (G_OBJECT (image)); + + gtk_image_clear (image); + + if (resource_path == NULL) + { + g_object_thaw_notify (G_OBJECT (image)); + return; + } + + stream = g_resources_open_stream (resource_path, 0, NULL); + if (stream != NULL) + { + pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, NULL); + g_object_unref (stream); + } + + if (pixbuf == NULL) + { + gtk_image_set_from_stock (image, + GTK_STOCK_MISSING_IMAGE, + DEFAULT_ICON_SIZE); + g_object_thaw_notify (G_OBJECT (image)); + return; + } + + gtk_image_set_from_pixbuf (image, pixbuf); + + g_object_unref (pixbuf); + + g_object_thaw_notify (G_OBJECT (image)); +} + + /** * gtk_image_set_from_pixbuf: * @image: a #GtkImage diff --git a/gtk/gtkimage.h b/gtk/gtkimage.h index ec8d243f94..e3b9714802 100644 --- a/gtk/gtkimage.h +++ b/gtk/gtkimage.h @@ -111,6 +111,7 @@ GType gtk_image_get_type (void) G_GNUC_CONST; GtkWidget* gtk_image_new (void); GtkWidget* gtk_image_new_from_file (const gchar *filename); +GtkWidget* gtk_image_new_from_resource (const gchar *resource_path); GtkWidget* gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf); GtkWidget* gtk_image_new_from_stock (const gchar *stock_id, GtkIconSize size); @@ -125,6 +126,8 @@ GtkWidget* gtk_image_new_from_gicon (GIcon *icon, void gtk_image_clear (GtkImage *image); void gtk_image_set_from_file (GtkImage *image, const gchar *filename); +void gtk_image_set_from_resource (GtkImage *image, + const gchar *resource_path); void gtk_image_set_from_pixbuf (GtkImage *image, GdkPixbuf *pixbuf); void gtk_image_set_from_stock (GtkImage *image, -- 2.30.2